//
//  EMainView.m
//  AppRanking
//
//  Created by Gang.Wang on 12-11-9.
//  Copyright (c) 2012年 Gang.Wang. All rights reserved.
//

#import "EMainView.h"
#import "EAppSetView.h"
#import "mcf.h"
#import "EAppSumChartPanl.h"

#import "JSONKit.h"
#import "EHttpRequestUtil.h"
#import "ERequestAppInfo.h"
#import "EAppInfo.h"
#import "ERequestAppRanking.h"
#import "ERanking.h"
#import "EGlobalAppData.h"
#import "EAppDetailRankingView.h"

#define _REQUEST_TYPE_GETAPPINFO_                       @"get_app_info"
#define _REQUEST_TYPE_GETAPPRANKING_                    @"get_app_ranking"

@interface EMainView ()<EAppSetViewDelegate, YDownloadRequestDelegate, EAppSumChartPanlDelegate, EAppDetailRankingViewDelegate>
{
    EAppSetView * appSetView_;
    EAppSumChartPanl * appSumChartPanl_;
    
    YDownloaderHelper * downloadHelper_;
    BOOL isDetailModel;
    
    
}

@property (nonatomic, strong) EAppSetView * appSetView;
@property (nonatomic, strong) EAppSumChartPanl * appSumChartPanl;

@property (nonatomic, strong) YDownloaderHelper * downloadHelper;
@property (nonatomic, retain) EAppInfo * currentAppData;

@property (nonatomic, retain) EAppDetailRankingView * appDetailRankingView;

@end

@implementation EMainView

@synthesize appSetView = appSetView_;
@synthesize appSumChartPanl = appSumChartPanl_;

@synthesize downloadHelper = downloadHelper_;


- (void) dealloc
{
    YRelease(appSetView_)
    YRelease(appSumChartPanl_)
    
    
    YRelease(downloadHelper_)
    
    [super dealloc];
}


- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        [self privateInit];
    }
    return self;
}


- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    YDownloaderHelper * temp_downloadHelper = [[YDownloaderHelper alloc] init];
    [temp_downloadHelper setTimeout:20];
    [temp_downloadHelper setMaxConcurrentTaskCount:5];
    self.downloadHelper = temp_downloadHelper;
    YRelease(temp_downloadHelper)
    
    
    EAppSetView * temp_appSetView = [[EAppSetView alloc] init];
    [temp_appSetView setDelegate:self];
    
    self.appSetView = temp_appSetView;
    YRelease(temp_appSetView)
    [self addSubview:self.appSetView];
    
    EAppSumChartPanl * temp_appSumPanl = [[EAppSumChartPanl alloc] init];
    temp_appSumPanl.delegate = self;
    
    self.appSumChartPanl = temp_appSumPanl;
    YRelease(temp_appSumPanl)
    [self addSubview:self.appSumChartPanl];
    
    
    EAppDetailRankingView * temp_detailRankingView = [[EAppDetailRankingView alloc] init];
    [temp_detailRankingView setDelegate:self];
    
    self.appDetailRankingView = temp_detailRankingView;
    YRelease(temp_detailRankingView);
    [self addSubview:self.appDetailRankingView];
    
    [self reAdjustLayout];
    self.bInited = TRUE;
}


- (void) reAdjustLayout
{
    
    if(!isDetailModel)
    {
        [self.appSetView setFrame:CGRectMake(0,
                                             0,
                                             self.width,
                                             self.height / 2 - 80)];
        
        [self.appSumChartPanl setFrame:CGRectMake(0,
                                                  self.height / 2 - 80,
                                                  self.width,
                                                  self.height - self.appSetView.height)];
        
        
        [self.appDetailRankingView setFrame:CGRectMake(self.width, 0, self.width, self.height)];
    }
    else
    {
        [self.appSetView setFrame:CGRectMake(self.width,
                                             0,
                                             self.width,
                                             self.height / 2 - 80)];
        
        [self.appSumChartPanl setFrame:CGRectMake(self.width,
                                                  self.height / 2 - 80,
                                                  self.width,
                                                  self.height - self.appSetView.height)];
        
        
        [self.appDetailRankingView setFrame:CGRectMake(0, 0, self.width, self.height)];
    }   
    
}

#pragma mark - Function Method
- (void) beginAddAppToSpy:(NSString *) appid
{
    if ([NSString isNilOrEmpty:appid])
    {
        return;
    }
    
    ERequestAppInfo * request = [[ERequestAppInfo alloc] init];
    request.name = _REQUEST_TYPE_GETAPPINFO_;
    [request setDelegate:self];
    [request setAppId:@"488101334"];
    
    [[EHttpRequestUtil sharedInstance] addDownloadRequest:request];
    [LeveyHUD showHUDAddedTo:self withText:@"正在获取 app 信息..."];
}


- (void) sendRankingRequest:(EAppInfo *) appInfo
{
    if(!appInfo)
    {
        return;
    }
    
    self.currentAppData = appInfo;
    
    ERequestAppRanking * request = [[ERequestAppRanking alloc] initWith:appInfo country:nil day:0 isiPhoneOs:appInfo.currentRequestPlatfromIsiPhone];
    request.name = _REQUEST_TYPE_GETAPPRANKING_;
    [request setDelegate:self];
    [[EHttpRequestUtil sharedInstance] addDownloadRequest:request];
    
    [LeveyHUD showHUDAddedTo:self withText:@"正在获取最新的排名信息..."];
}

- (void) getAppInfoOk:(ERequestAppInfo *) request
{
    if (!request)
    {
        return;
    }
    
    NSData * appData = request.recvData.data;
    if (!appData)
    {
        return;
    }
    
    // to do ...
    NSString * appInfoFilePath = [EGlobalAppData appDataFilePath:request.appId];
    
    if ([YFile fileExistsAtPath:appInfoFilePath])
    {
        [YFile deleteFileAtPath:appInfoFilePath];
    }
    
    NSDictionary * result = [appData objectFromJSONData];
    NSData *data = [NSPropertyListSerialization dataFromPropertyList:result
                                                              format:kCFPropertyListXMLFormat_v1_0
                                                    errorDescription:nil];
    
    [YFile createFileAtPath:appInfoFilePath withData:data andAttributes:nil];
    
    EAppInfo * app = [[EAppInfo alloc] initWithJsonData:appData];
    
    [self.appSetView showApp:app];
    
    [[EGlobalAppData sharedInstance] spyNewApp:request.appId];
    
    [self sendRankingRequest:app];
    
    [LeveyHUD hideHUDForView:self withText:[NSString stringWithFormat:@"获取 %@ 信息成功", request.appId]];
}

- (void) getAppInfoFailed:(NSString *) appId
{
    if ([NSString isNilOrEmpty:appId])
    {
        return;
    }
    
    [LeveyHUD hideHUDForView:self withText:[NSString stringWithFormat:@"获取 %@ 信息失败, 请确认 app id 无误后重试", appId]];
}


- (void) getAppRankingInfoOk:(DownloadRequest *) request
{
    if (!request)
    {
        [LeveyHUD hideHUDForView:self withText:@"获取最新排名数据完成"];
        return;
    }
    
    NSData * rankData = request.recvData.data;
    if (!rankData)
    {
        [LeveyHUD hideHUDForView:self withText:@"获取最新排名数据完成"];
        return;
    }
    
    ERanking * appRanking = [[ERanking alloc] initWithData:rankData];
    if (!appRanking)
    {
        [LeveyHUD hideHUDForView:self withText:@"获取最新排名数据完成"];
        return;
    }

    // to do ... show appRanking info to view
    [self.appSumChartPanl setAppRanking:appRanking];
    
    YRelease(appRanking);
    
    [LeveyHUD hideHUDForView:self withText:@"获取最新排名数据完成"];
}

- (void) getAppRankingInfoFailed
{
    [LeveyHUD hideHUDForView:self withText:@"获取最新排名数据失败"];
}


#pragma mark - EAppSetDelegate
- (void) addAppToSpy:(NSString *)appid
{
    if ([NSString isNilOrEmpty:appid])
    {
        return;
    }
    
    [self beginAddAppToSpy:appid];
}

- (void) need2Download:(DownloadRequest *)request
{
    if (!request)
    {
        return;
    }
    
    [self.downloadHelper addDownloadRequest:request];
}

- (void) appClicked:(EAppInfo *) appInfo
{
    if (!appInfo)
    {
        return;
    }
    
    [self sendRankingRequest:appInfo];
}


#pragma mark - DownloadRequestDelegagte
- (void) didTaskReceiveDataFinish:(NSData *)theData request:(DownloadRequest *)request
{
    if ([request.name isEqualToString:_REQUEST_TYPE_GETAPPINFO_])
    {
        [self performSelectorOnMainThread:@selector(getAppInfoOk:) withObject:request waitUntilDone:NO];
    }
    else if ([request.name isEqualToString:_REQUEST_TYPE_GETAPPRANKING_])
    {
        [self performSelectorOnMainThread:@selector(getAppRankingInfoOk:) withObject:request waitUntilDone:NO];
    }
        
}

- (void) taskDataDownloadFailed:(NSError *)error request:(DownloadRequest *)request
{
    if ([request.name isEqualToString:_REQUEST_TYPE_GETAPPINFO_])
    {
        [self performSelectorOnMainThread:@selector(getAppInfoFailed:) withObject:[(ERequestAppInfo *) request appId] waitUntilDone:NO];
    }
    else if ([request.name isEqualToString:_REQUEST_TYPE_GETAPPRANKING_])
    {
        [self performSelectorOnMainThread:@selector(getAppRankingInfoFailed) withObject:nil waitUntilDone:NO];
    }
}

#pragma mark - EAppSumChartPanlDelegate
- (void) detailRanking:(id)obj
{
    isDetailModel = TRUE;
    
    [self.appDetailRankingView setAppInfo:self.currentAppData];
    [self reAdjustLayout];
}


#pragma mark - EAppDetailRankingViewDelegate
- (void) closeDetailRankingView:(id)obj
{
    isDetailModel = NO;
    
    [self reAdjustLayout];
}

@end
